package com.zhihui.online.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.zhihui.online.dto.teacher.TeacherInfoDTO;
import com.zhihui.online.dto.teacher.TeacherPageDTO;
import com.zhihui.online.entity.Teacher;
import com.zhihui.online.exception.MyCustomerException;
import com.zhihui.online.service.TeacherService;
import com.zhihui.online.utils.MD5Helper;
import com.zhihui.online.utils.Result;
import com.zhihui.online.utils.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 教师表 前端控制器
 * </p>
 *
 * @author 程序员阿良,21岁纯情后爸,懒得想名字
 * @since 2024-08-01
 */
@Api(tags = "教师管理控制器")
@RestController
@RequestMapping("/online/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @ApiOperation("教师分页列表")
    @GetMapping("/{page}/{limit}")
    public Result listPagedUsers(
            @PathVariable Long page,
            @PathVariable Long limit,

            TeacherPageDTO teacherPageDTO
    ) {
        IPage<Teacher> iPage = new Page<>(page,limit);
        iPage = this.teacherService.listPagedTeachers(iPage, teacherPageDTO);
        return Result.ok(iPage);
    }

    @ApiOperation("添加教师")
    @PostMapping("/addTeacher")
    public Result addUser(@RequestBody Teacher teacher){


        String s = MD5Helper.encrypt(teacher.getPassword());

        teacher.setPassword(s);

        boolean b = this.teacherService.save(teacher);

        return b?Result.ok():Result.fail();
    }

    @ApiOperation("删除教师信息")
    @DeleteMapping("/removeTeacherById/{id}")
    public Result removeUserById(@PathVariable Long id){
        boolean b = this.teacherService.removeById(id);
        return b?Result.ok():Result.fail();
    }


    @ApiOperation("批量删除教师信息")
    @DeleteMapping("/removeBatchTeacherByIds")
    public Result removeBatchUserByIds(@RequestBody List<Long> ids){
        boolean b = this.teacherService.removeByIds(ids);
        return b?Result.ok():Result.fail();
    }

    @ApiOperation("根据id获取教师")
    @GetMapping("/findTeacherById/{id}")
    public Result getRoleById(@PathVariable Long id){
        Teacher teacher = this.teacherService.getById(id);
        return (teacher!=null)?Result.ok(teacher):Result.fail();
    }

    @ApiOperation("修改教师信息")
    @PostMapping("/updateTeacher")
    public Result updateUser(@RequestBody TeacherInfoDTO teacherInfoDTO){
        //字段非空判断
        if (teacherInfoDTO == null || teacherInfoDTO.getId() == null) {
            throw new MyCustomerException(ResultCodeEnum.DATA_ERROR);
        }
        //转换为实体类
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacherInfoDTO, teacher);
        boolean isSuccess = this.teacherService.updateById(teacher);
        return isSuccess ? Result.ok() : Result.fail();
    }

    @ApiOperation("更改教师状态")
    @GetMapping("/updateStatus/{id}/{status}")
    public Result<Object> updateStatus(@PathVariable Long id,
                                       @PathVariable Integer status) {
        if (id == null || status == null) {
            return Result.fail().message("参数缺失！");
        }
        this.teacherService.updateStatus(id, status);
        return Result.ok();
    }

//    @ApiOperation("更改教师状态")
//    @GetMapping("/updateStatus/{password}")
//    public Result<Object> updateStatus(@PathVariable Long id,
//                                       @PathVariable Integer status) {
//        if (id == null || status == null) {
//            return Result.fail().message("参数缺失！");
//        }
//        this.teacherService.updateStatus(id, status);
//        return Result.ok();
//    }


}

